from os.path import getsize
from mmap import mmap, ACCESS_READ

def name_from_path(i):
	tmp = i.split('/')
	tmp2 = tmp[len(tmp) - 1].split('\\')
	return tmp2[len(tmp2) - 1]

def getbounds(fpath, chunksize):
	# open the file + mmap
	f = open(fpath, 'rb')
	m = mmap(f.fileno(), 0, access=ACCESS_READ)

	origsize = getsize(fpath)
	# is there even a need to split?
	if chunksize == 0 or origsize <= chunksize:
		return [{'f': f, 'm': m, 'path': fpath, 'name': name_from_path(fpath), 'begin': 0, 'end': origsize, 'length': origsize}]
	#
	parts = origsize / chunksize
	lastpartsize = origsize % chunksize
	#
	digits = len(str(parts))
	if lastpartsize != 0:
		digits = len(str(parts + 1))
	if digits < 3:
		digits = 3
	#
	fname = name_from_path(fpath)
	result = []	
	# note: begin + end is in python slice convention (eg. data[begin:end] will be length long)
	for i in xrange(parts):
		result.append({'f': f, 'm': m, 'path': fpath, 'name': fname + '.' + str(i + 1).zfill(digits), 'begin': chunksize * i, 'end': chunksize * (i + 1), 'length': chunksize})
	if lastpartsize != 0:
		result.append({'f': f, 'm': m, 'path': fpath, 'name': fname + '.' + str(parts + 1).zfill(digits), 'begin': chunksize * parts, 'end': origsize, 'length': origsize - chunksize * parts})
	return result